package com.example.mannortycoon.uitl;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import java.util.Calendar;
import java.util.Map;

public class JWTUtils {
    private static final String SING = "!@#$%^&*()";

    public static String getToken(Map<String, Object> map) {
        //设置过期时间
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DATE, 7);
        //创建jwt builder
        JWTCreator.Builder builder = JWT.create();
        map.forEach((k, v) -> builder.withClaim(k, String.valueOf(v)));
        String token = builder.withExpiresAt(instance.getTime()).sign(Algorithm.HMAC256(SING));
        return token;

    }

    public static DecodedJWT verify(String token) {
        return JWT.require(Algorithm.HMAC256(SING)).build().verify(token);
    }
    public static Long getId(String token){
        try{
            String id = JWTUtils.verify(token).getClaim("id").asString();
            return Long.valueOf(id);
        }catch (Exception e){
            throw new RRException("用户登录信息缺失", e);
        }
    }
}
